package com.common.www.service.project;

import org.springframework.boot.base.jpa.service.BaseService;
import com.common.www.bean.entity.project.Company;
import com.common.www.repository.project.CompanyRepository;
import com.common.www.utils.TxtUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by cwx183898 on 2017/12/14.
 */
@Service
@Transactional
public class CompanyService extends BaseService<Company, Long> {
    @Autowired
    private CompanyRepository repository;

    @Override
    public CompanyRepository getRepository() {
        return repository;
    }

    /**
     * 逻辑查询
     */
    public Company getById(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("flag", 1);
        List<Company> list = queryToList(map, null);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    /**
     * 逻辑查询
     */
    public List<Company> getList(Map<String, Object> map, Pageable pageable) {
        if (map == null)
            map = new HashMap<>();
        map.put("flag", 1);
        List<Company> list = queryToList(map, pageable);
        return list;
    }

    /**
     * 逻辑查询
     */
    public Page<Company> getListPage(Map<String, Object> map, Pageable pageable) {
        if (map == null)
            map = new HashMap<>();
        map.put("flag", 1);
        Page<Company> page = query(map, pageable);
        return page;
    }

    /**
     * 逻辑批量删除
     */
    public void deleteByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            deleteById(ids[i]);
        }
    }

    /**
     * 逻辑删除 节点，和所有相关子节点
     */
    public void deleteById(Long id) {
        Company res = findOne(id);
        if (res != null) {
            res.setFlag(0);
            save(res);
        }
    }

    /**
     * 根据名称查询
     */
    public Company getByName(String name) {
        if (TxtUtil.isEmpty(name))
            return null;
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("flag", 1);
        List<Company> list = queryToList(map, null);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }


    ////////////////////////////////

    /**
     * 递归查询节点的地址
     */
    public String getNodePath(Company sceneryShiftScopeCheckMajor, String path) {
        path += "/" + sceneryShiftScopeCheckMajor.getName();
        if (sceneryShiftScopeCheckMajor.getParent() != null)
            return getNodePath(sceneryShiftScopeCheckMajor.getParent(), path);
        return path;
    }

    /**
     * 递归查询子节点的地址
     */
    public void setChildNodePath(List<Company> children) {
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                Company sceneryShiftScopeCheckMajor = children.get(i);
                if (sceneryShiftScopeCheckMajor.getChildren() != null)
                    setChildNodePath(sceneryShiftScopeCheckMajor.getChildren());
                sceneryShiftScopeCheckMajor.setNodePath(getNodePath(sceneryShiftScopeCheckMajor, ""));
            }
        }
    }

    /**
     * 递归查询反向排序所有节点的notePath
     */
    public void sortChildNodePath(List<Company> children) {
        if (children != null)
            for (int i = 0; i < children.size(); i++) {
                Company sceneryShiftScopeCheckMajor = children.get(i);

                sortType(sceneryShiftScopeCheckMajor);

            }
    }

    /**
     * 颠倒排序nodePath的字段
     */
    public void sortType(Company projectType) {
        if (projectType.getChildren() != null)
            sortChildNodePath(projectType.getChildren());

        String path = projectType.getNodePath();
        if (TxtUtil.isEmpty(path))
            return;
        String newPath = "";
        //路劲结果拆分后反向排序，除去第一个节点路劲(自己本身的节点)
        String res[] = path.split("/");
        for (int j = res.length - 1; j > 1; j--) {
            newPath += "/" + res[j];
        }
        if (TxtUtil.isEmpty(newPath)) {
            newPath += "/";
        }
        projectType.setNodePath(newPath + (newPath.equals("/") ? "" : "/") + projectType.getName());
    }

    /**
     * 提取该节点下的所有节点
     */
    public void getChildBlFlat(List<Company> children, List<Company> all) {
        if (children != null)
            for (int i = 0; i < children.size(); i++) {
                Company sceneryShiftScopeCheckMajor = children.get(i);
                if (sceneryShiftScopeCheckMajor.getChildren() != null)
                    getChildBlFlat(sceneryShiftScopeCheckMajor.getChildren(), all);
                all.add(sceneryShiftScopeCheckMajor);
            }
    }

    /**
     * 设置它自己的节点等级
     */
    public int getTypeLevel(Company paperType) {
        if (paperType == null) {
            return 1;
        }
        if (paperType.getParent() == null) {
            return 1;
        } else {
            return paperType.getParent().getLevel() + 1;
        }
    }
}
